import 'package:flutter/cupertino.dart';
import 'package:flutter/material.dart';
import 'package:flutter/services.dart';
import 'package:flutter_demo/tmac/icon/IconFont.dart';
import 'package:flutter_demo/tmac/util/AndroidFlutterPlugin.dart';
import 'package:flutter_demo/tmac/util/HttpUtil.dart';
import 'package:flutter_demo/tmac/util/LocationUtil.dart';
import 'package:flutter_demo/tmac/util/SystemUtil.dart';
import 'package:flutter_demo/tmac/widget/AllDrawableTextWidget2.dart';
import 'package:flutter_demo/tmac/widget/CircleWidget.dart';
import 'package:flutter_demo/tmac/widget/MyListTile2.dart';
import 'package:flutter_demo/tmac/widget/SimpleBannerWidget.dart';
import 'package:fluttertoast/fluttertoast.dart';
import 'package:geolocator/geolocator.dart';

class HomePageStatefulWidget extends StatefulWidget {
  @override
  State<StatefulWidget> createState() {
    return HomePageState();
  }
}

class HomePageState extends State<HomePageStatefulWidget>
    with AutomaticKeepAliveClientMixin {
  List<Widget> list = [];
  List<String> images = [
    "https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=2196884168,2008099733&fm=26&gp=0.jpg",
    "https://ss1.bdstatic.com/70cFvXSh_Q1YnxGkpoWK1HF6hhy/it/u=1873147832,1132611521&fm=26&gp=0.jpg",
    "https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=1813641746,2528487936&fm=26&gp=0.jpg",
    "https://ss0.bdstatic.com/70cFuHSh_Q1YnxGkpoWK1HF6hhy/it/u=1070982179,1357572299&fm=26&gp=0.jpg"
  ];
  String city = "正在定位";
  String nowWeather = " ";

  @override
  void initState() {
    super.initState();
    SystemUtil.statusColorBar(color: Colors.blue);
    initPageData();
    // initCity();
    initWeather();
  }

  @override
  Widget build(BuildContext context) {
    return Scaffold(
        body: SafeArea(
      child: Column(
        children: <Widget>[
          Container(
            padding: EdgeInsets.only(top: 10, bottom: 20),
            color: Colors.blue,
            child: Column(
              mainAxisSize: MainAxisSize.min,
              children: <Widget>[
                Row(
                  mainAxisSize: MainAxisSize.max,
                  mainAxisAlignment: MainAxisAlignment.spaceBetween,
                  children: <Widget>[
                    Padding(padding: EdgeInsets.only(left: 10)),
                    MyListTile2(
                        subTitle: Text(
                          nowWeather,
                          style: TextStyle(color: Colors.white70, fontSize: 9),
                        ),
                        trailing: Icon(
                          Icons.keyboard_arrow_down,
                          color: Colors.white,
                        ),
                        title: new GestureDetector(
                          child: Text(
                            city,
                            style: TextStyle(
                              color: Colors.white,
                              fontSize: 14,
                            ),
                          ),
                          onTap: () {
                            AndroidFlutterPlugin.instance.openGps();
                          },
                        )),
                    Padding(padding: EdgeInsets.only(left: 10)),
                    Expanded(
                      child: ConstrainedBox(
                        constraints: BoxConstraints(maxHeight: 33),
                        child: TextField(
                          maxLines: 1,
                          keyboardType: TextInputType.text,
                          decoration: InputDecoration(
                            prefixIcon: Icon(
                              Icons.search,
                              color: Colors.grey[600],
                              size: 20,
                            ),
                            suffixIcon: Icon(
                              Icons.keyboard_voice,
                              color: Colors.grey[600],
                              size: 20,
                            ),
                            contentPadding: EdgeInsets.only(
                              left: 10,
                              right: 10,
                            ),
                            hintText: "厦门公积金",
                            filled: true,
                            fillColor: Colors.white,
                            hintStyle: TextStyle(color: Colors.grey[500]),
                            border: OutlineInputBorder(
                                borderRadius: BorderRadius.circular(5),
                                borderSide: BorderSide.none,
                                gapPadding: 1),
                          ),
                        ),
                      ),
                    ),
                    Padding(padding: EdgeInsets.only(left: 10)),
                    GestureDetector(
                      child: Icon(
                        Icons.add_circle_outline,
                        color: Colors.white,
                      ),
                      onTap: () {
                        Fluttertoast.showToast(
                            msg: "敬请期待", gravity: ToastGravity.CENTER);
                      },
                    ),
                    Padding(padding: EdgeInsets.only(right: 10)),
                  ],
                ),
                Padding(padding: EdgeInsets.only(top: 15)),
                Row(
                  children: <Widget>[
                    Expanded(
                      child: AllDrawableTextWidget2(
                        paddingTop: 3,
                        title: Text(
                          "扫一扫",
                          style: TextStyle(color: Colors.white),
                        ),
                        iconTop: Icon(IconFont.saoyisao,
                            size: 25, color: Colors.white),
                      ),
                    ),
                    Expanded(
                      child: AllDrawableTextWidget2(
                        paddingTop: 3,
                        title: Text(
                          "付款/收钱",
                          style: TextStyle(color: Colors.white),
                        ),
                        iconTop: Icon(
                          IconFont.fukuan,
                          size: 25,
                          color: Colors.white,
                        ),
                      ),
                    ),
                    Expanded(
                      child: AllDrawableTextWidget2(
                        paddingTop: 3,
                        title: Text(
                          "出行",
                          style: TextStyle(color: Colors.white),
                        ),
                        iconTop: Icon(IconFont.chuxing,
                            size: 25, color: Colors.white),
                      ),
                    ),
                    Expanded(
                        child: AllDrawableTextWidget2(
                      paddingTop: 3,
                      title: Text(
                        "卡包",
                        style: TextStyle(color: Colors.white),
                      ),
                      iconTop:
                          Icon(IconFont.kabao, size: 25, color: Colors.white),
                    ))
                  ],
                ),
              ],
            ),
          ),
          Expanded(
            child: Container(
              color: Colors.grey[50],
              child: SingleChildScrollView(
                  child: Container(
                child: Column(
                  children: <Widget>[
                    Padding(
                      padding: EdgeInsets.only(left: 10, right: 10, top: 5),
                      child: GridView.builder(
                          shrinkWrap: true,
                          physics: NeverScrollableScrollPhysics(),
                          itemCount: list.length,
                          gridDelegate:
                              SliverGridDelegateWithFixedCrossAxisCount(
                                  crossAxisCount: 5),
                          itemBuilder: (context, index) {
                            return list[index];
                          }),
                    ),
                    //中间卡券部分
                    Padding(
                      padding: EdgeInsets.only(left: 10, right: 10, bottom: 2),
                      child: Card(
                        elevation: 0.5,
                        child: Padding(
                          padding: EdgeInsets.only(
                              left: 12, right: 8, bottom: 13, top: 13),
                          child: Stack(
                            alignment: Alignment.centerLeft,
                            children: <Widget>[
                              Align(
                                alignment: Alignment.centerRight,
                                child: AllDrawableTextWidget2(
                                    title: Icon(
                                      Icons.keyboard_arrow_right,
                                      color: Colors.grey,
                                    ),
                                    iconLeft: CircleWidget(
                                      color: Colors.red,
                                      width: 8,
                                      height: 8,
                                    )),
                              ),
                              Column(
                                crossAxisAlignment: CrossAxisAlignment.start,
                                mainAxisAlignment: MainAxisAlignment.end,
                                children: <Widget>[
                                  RichText(
                                      text: TextSpan(
                                          text: '你有一张卡券即将过期',
                                          style: TextStyle(
                                              color: Colors.black,
                                              fontSize: 13),
                                          children: <TextSpan>[
                                        TextSpan(
                                            text: '    3小时前',
                                            style: TextStyle(
                                                color: Colors.grey,
                                                fontSize: 13)),
                                      ])),
                                  Padding(padding: EdgeInsets.only(top: 7)),
                                  RichText(
                                      text: TextSpan(
                                          text: '快递取件通知',
                                          style: TextStyle(
                                              color: Colors.black,
                                              fontSize: 13),
                                          children: <TextSpan>[
                                        TextSpan(
                                            text: '    3小时前',
                                            style: TextStyle(
                                                color: Colors.grey,
                                                fontSize: 13)),
                                      ])),
                                ],
                              )
                            ],
                          ),
                        ),
                      ),
                    ),
                    //banner
                    Padding(
                        padding:
                            EdgeInsets.only(left: 10, right: 10, bottom: 2),
                        child: Card(
                          elevation: 0.5,
                          child: Padding(
                            padding: EdgeInsets.all(2),
                            child: Container(
                                height: 100,
                                child: SimpleBannerWidget(
                                  images: images,
                                  selectIndicatorHeight: 4,
                                  unSelectIndicatorHeight: 4,
                                  unSelectIndicatorWidth: 4,
                                )),
                          ),
                        )),
                    Padding(
                      padding: EdgeInsets.only(left: 10, right: 10, bottom: 10),
                      child: Card(
                        elevation: 0.5,
                        child: Padding(
                          padding: EdgeInsets.all(8),
                          child: Column(
                            children: <Widget>[
                              Row(
                                mainAxisAlignment:
                                    MainAxisAlignment.spaceBetween,
                                children: <Widget>[
                                  Text(
                                    "财富生活",
                                    style: TextStyle(
                                        fontSize: 15,
                                        fontWeight: FontWeight.bold),
                                  ),
                                  Text(
                                    "热点|金融数据超预期",
                                    style: TextStyle(color: Colors.grey),
                                  )
                                ],
                              ),
                              Padding(padding: EdgeInsets.only(bottom: 10)),
                              Row(
                                children: <Widget>[
                                  Expanded(
                                    child: AllDrawableTextWidget2(
                                        title: Text("今日好货"),
                                        paddingTop: 10,
                                        iconTop: ClipRRect(
                                          borderRadius:
                                              BorderRadius.circular(5),
                                          child: Image.network(
                                            "https://ss0.bdstatic.com/70cFvHSh_Q1YnxGkpoWK1HF6hhy/it/u=2474701707,3422680855&fm=15&gp=0.jpg",
                                            width: 100,
                                            height: 100,
                                          ),
                                        )),
                                  ),
                                  Padding(padding: EdgeInsets.only(right: 10)),
                                  Expanded(
                                    child: AllDrawableTextWidget2(
                                        title: Text("玩转理财"),
                                        paddingTop: 10,
                                        iconTop: ClipRRect(
                                          borderRadius:
                                              BorderRadius.circular(5),
                                          child: Image.network(
                                            "https://ss2.bdstatic.com/70cFvnSh_Q1YnxGkpoWK1HF6hhy/it/u=3127360182,2117867730&fm=26&gp=0.jpg",
                                            width: 100,
                                            height: 100,
                                            fit: BoxFit.fitHeight,
                                          ),
                                        )),
                                  ),
                                  Padding(padding: EdgeInsets.only(right: 10)),
                                  Expanded(
                                    child: AllDrawableTextWidget2(
                                        title: Text("天天领福利"),
                                        paddingTop: 10,
                                        iconTop: ClipRRect(
                                          borderRadius:
                                              BorderRadius.circular(5),
                                          child: Image.network(
                                            "https://gimg2.baidu.com/image_search/src=http%3A%2F%2F5b0988e595225.cdn.sohucs.com%2Fimages%2F20171221%2F8ad946dca57b41a8bbdc41c1f9fd3f0b.gif&refer=http%3A%2F%2F5b0988e595225.cdn.sohucs.com&app=2002&size=f9999,10000&q=a80&n=0&g=0n&fmt=jpeg?sec=1617940748&t=7043419395642d0b77a0ee33af8acc52",
                                            width: 100,
                                            height: 100,
                                            fit: BoxFit.fill,
                                          ),
                                        )),
                                  )
                                ],
                              )
                            ],
                          ),
                        ),
                      ),
                    )
                  ],
                ),
              )),
            ),
          )
        ],
      ),
    ));
  }

  @override
  bool get wantKeepAlive => true;

  void initPageData() {
    list.add(new GestureDetector(
      onTap: null,
      child: new AllDrawableTextWidget2(
          paddingTop: 3,
          title: Text("转账"),
          iconTop: Icon(
            IconFont.zhuanzhang,
            size: 30,
            color: Colors.blue,
          )),
    ));
    list.add(new GestureDetector(
      onTap: null,
      child: new AllDrawableTextWidget2(
          paddingTop: 3,
          title: Text("口碑"),
          iconTop: Icon(
            IconFont.koubei,
            size: 30,
            color: Colors.deepOrangeAccent,
          )),
    ));
    list.add(new GestureDetector(
      onTap: null,
      child: new AllDrawableTextWidget2(
          paddingTop: 3,
          title: Text("饿了么"),
          iconTop: Icon(
            IconFont.elme,
            size: 30,
            color: Colors.blue,
          )),
    ));
    list.add(new GestureDetector(
      onTap: null,
      child: new AllDrawableTextWidget2(
          paddingTop: 3,
          title: Text("余额宝"),
          iconTop: Icon(
            IconFont.yuebao,
            size: 30,
            color: Colors.red[600],
          )),
    ));
    list.add(new GestureDetector(
      onTap: null,
      child: new AllDrawableTextWidget2(
          paddingTop: 3,
          title: Text("芝麻信用"),
          iconTop: Icon(
            IconFont.zimaxinyong,
            size: 30,
            color: Color(0xff5495f6),
          )),
    ));
    list.add(new GestureDetector(
      onTap: null,
      child: new AllDrawableTextWidget2(
          paddingTop: 3,
          title: Text("城市服务"),
          iconTop: Icon(
            IconFont.chengshi,
            size: 30,
            color: Colors.blueAccent,
          )),
    ));
    list.add(new GestureDetector(
      onTap: null,
      child: new AllDrawableTextWidget2(
          paddingTop: 3,
          title: Text("滴滴出行"),
          iconTop: Icon(
            IconFont.didi,
            size: 30,
            color: Colors.orange,
          )),
    ));
    list.add(new GestureDetector(
      onTap: null,
      child: new AllDrawableTextWidget2(
          paddingTop: 3,
          title: Text("卡包"),
          iconTop: Icon(
            IconFont.kabao,
            size: 30,
            color: Colors.yellow[800],
          )),
    ));
    list.add(new GestureDetector(
      onTap: null,
      child: new AllDrawableTextWidget2(
          paddingTop: 3,
          title: Text("美团"),
          iconTop: Icon(IconFont.meituan, size: 30, color: Color(0xff07C4BD))),
    ));
    list.add(new GestureDetector(
      onTap: null,
      child: new AllDrawableTextWidget2(
          paddingTop: 3,
          title: Text("电影演出"),
          iconTop: Icon(
            IconFont.dianying,
            size: 30,
            color: Colors.deepOrangeAccent,
          )),
    ));
    list.add(new GestureDetector(
      onTap: null,
      child: new AllDrawableTextWidget2(
          paddingTop: 3,
          title: Text("充值"),
          iconTop: Icon(
            IconFont.chongzhi,
            size: 30,
            color: Colors.orange,
          )),
    ));
    list.add(new GestureDetector(
      onTap: null,
      child: new AllDrawableTextWidget2(
        paddingTop: 3,
        title: Text("更多"),
        iconTop: Icon(Icons.widgets, size: 30, color: Colors.grey[700]),
      ),
    ));
  }

  void initCity() async {
    var cityName = await LocationUtil.instance.getCityName();

    setState(() {
      city = cityName;
      print(city);
    });
  }

  void initWeather() async {
    dynamic weatherData = await HttpUtil.instance.getCityWeather();

    setState(() {
      var result = weatherData['result'];
      var today = result['today'];
      var cityName = weatherData['result']['today']['city'];
      var sk = result['sk'];
      String weather = today['weather'];
      String temperature = sk['temp'];

      city = cityName;
      nowWeather = "$weather$temperature";
    });
  }
}
